<template>
  <div class="comment">
     <h1>评论</h1>
     <el-input
        :autosize="{ minRows: 6, maxRows: 10}"
        type="textarea"
        :rows="2"
        class="textarea"
        placeholder="请输入内容"
        maxlength='300'
        resize='none'
        v-model="textarea">
        </el-input>
      <div class="combutton">
          <span v-if="this.$store.state.nologin" class="login" @click="login">登录</span>
          <span v-if="this.$store.state.nologin">后参与评论</span>
          <el-button class="commit" @click="sendcomment">提交评论</el-button>
      </div>
      <ul class="commentList">
          <li class="commentdetail" v-for="(item,index) in commentdata" :key="index">
             <p><span>{{item.username}}</span><span>{{item.creatimestr}}</span></p>
             <div>{{item.content}}</div>
          </li>
      </ul>
  </div>
</template>

<script>
import api from '../static/js/api.js';
export default {
  data(){
      return{
          textarea:''
      }
  },
  props: {
    commentdata:{
      type:Array,
      default:function(){
        return []
      }
    }
  },
  methods:{
      login(){
           this.$store.commit("changeshowlogin",true)
      },
      sendcomment(){
          if(this.textarea == ''){
                this.$alert('请输入评论内容');
                return false;
          }
          if(this.$store.state.nologin){
              this.$store.commit("changeshowlogin",true)
          }else{
            
             api.addNewsList({content:this.textarea,postId:this.$router.history.current.params.id,userId:this.$store.state.userinfo.id}).then((res)=>{
                            if(res.code == 1){
                               this.$alert('添加评论成功，请等待审核');
                            }else{
                               this.$alert(res.msg)
                            }
                 })
          }
      }
  }

};
</script>

<style scoped>
.comment{
    margin-top: 20px;
    padding:20px;
}
.comment h1{
    font-size: 22px;
    color: #333;
}
.textarea{
    margin-top: 20px;
}
.combutton{
    height: 36px;
    text-align: right;
    margin: 20px 0 0;
    font-size: 14px;
    color: #bbb;
}
.login{
    transition: all .2s ease;
    color: #333;
    font-size: 16px;
    margin-right: 4px;
    cursor: pointer;
}
.login:hover{
    color: #cd9c54;
    text-decoration: underline;
}
.commit{
    background: #cd9c54;
    color: #ffffff;
    margin-left: 10px;
}

.commentdetail{
    margin-bottom: 26px;
    border-bottom: 1px solid #e9edef;
    padding-bottom: 24px;
}
.commentdetail p span:nth-child(1){
    font-size: 16px;    
    margin-right: 8px;
    color: #333;
}
.commentdetail p span:nth-child(2){
    color: #bbbbbb;
    font-size: 12px;
}
.commentdetail div{
    font-size: 14px;
    line-height: 26px;
    margin-top: 12px;
    word-break: break-all;
}
</style>

